package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.entity.Category;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CategoryIdAndNameVo;
import com.cskaoyan.bean.vo.CategoryInfoVo;
import com.cskaoyan.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  商品类目Controller
 *
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     *  查询所有的商品类目
     *
     */
    @RequestMapping("list")
    public BaseRespVo list(){
        BasePageData<CategoryInfoVo> categoryList = categoryService.query();
        return BaseRespVo.ok(categoryList);
    }

    /**
     *  查询所有的类目名信息
     *  l1中的value和label属性就是category中的id和name
     *  l1请求是获取一级标题的id和name
     */
    @RequestMapping("l1")
    public BaseRespVo l1(){
        BasePageData<CategoryIdAndNameVo> categoryNameInfoVos = categoryService.selectNameAndId();
        return BaseRespVo.ok(categoryNameInfoVos);
    }

    /**
     *  新增类目信息
     *
     */
    @RequestMapping("create")
    public BaseRespVo add(@RequestBody Category category){
        Category categoryResult = categoryService.insert(category);
        return BaseRespVo.ok(categoryResult);
    }


    /**
     *  编辑商品类目信息
     *
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Category category){
        categoryService.update(category);
        return BaseRespVo.ok();
    }


    /**
     *  删除商品类目
     *
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Category category){
        categoryService.delete(category);
        return BaseRespVo.ok();
    }

}
